package com.example.myproject.service.serviceImpl;


import com.example.myproject.model.Customer;
import com.example.myproject.repository.CustomerRepository;
import com.example.myproject.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class CustomerServiceImpl implements CustomerService {

    private final CustomerRepository customerRepository;
    private final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    public CustomerServiceImpl(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @Override
    public List<Customer> getAllCustomers() {
        log.debug("正在获取所有客户信息");
        List<Customer> customers = customerRepository.findAll();
        log.debug("获取到{}条客户记录", customers.size());
        return customers;
    }

    @Override
    public List<Customer> getCustomerByName(String name) {
        log.debug("尝试根据名称模糊搜索客户，搜索关键字：{}", name);
        List<Customer> customers = customerRepository.findByNameContaining(name);
        if (customers.isEmpty()) {
            log.debug("没有找到符合条件的客户，关键字：{}", name);
        } else {
            log.debug("找到{}条符合条件的客户记录，关键字：{}", customers.size(), name);
        }
        return customers;
    }


    @Override
    public Customer addCustomer(Customer customer) {
        log.debug("正在添加新客户，客户详情：{}", customer);
        customerRepository.save(customer);
        log.debug("新客户添加成功，客户ID：{}", customer.getId());
        return customer;
    }
    @Override
    public Customer updateCustomer(String name, Customer customerDetails) {
        log.debug("正在更新客户信息，原客户名：{}", name);
        Optional<Customer> optionalCustomer = Optional.ofNullable(customerRepository.findByName(name));
        if (optionalCustomer.isPresent()) {
            Customer customer = optionalCustomer.get();
            log.debug("找到客户，正在更新信息，客户ID：{}", customer.getId());
            // 更新客户信息
            if (customerDetails.getName() != null) {
                customer.setName(customerDetails.getName());
            }
            if (customerDetails.getPhone() != null) {
                customer.setPhone(customerDetails.getPhone());
            }
            if (customerDetails.getAddress() != null) {
                customer.setAddress(customerDetails.getAddress());
            }
            if (customerDetails.getFax() != null) {
                customer.setFax(customerDetails.getFax());
            }
            if (customerDetails.getEmail() != null) {
                customer.setEmail(customerDetails.getEmail());
            }
            if (customerDetails.getBankName() != null) {
                customer.setBankName(customerDetails.getBankName());
            }
            if (customerDetails.getBankAccount() != null) {
                customer.setBankAccount(customerDetails.getBankAccount());
            }
            if (customerDetails.getRemarks() != null) {
                customer.setRemarks(customerDetails.getRemarks());
            }

            log.debug("客户信息更新成功，详情：{}",customerRepository.save(customer));
            return customerRepository.save(customer);
        } else {
            log.debug("未找到名称为 {} 的客户，更新失败", name);
            return null;
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCustomer(String name) {

        log.debug("正在删除客户，客户名：{}", name);
        customerRepository.deleteByName(name);
        log.debug("客户名为 {} 的客户已被删除", name);
    }
}
